class WebSocketService {
  constructor(url) {
    this.url = url
    this.socket = null
    this.isConnected = false
    this.reconnectAttempts = 0
    this.maxReconnectAttempts = 5
  }

  connect(token) {
    if (this.isConnected) return
    
    this.socket = new WebSocket(`${this.url}?token=${token}`)
    
    this.socket.onopen = () => {
      this.isConnected = true
      this.reconnectAttempts = 0
      console.log('WebSocket连接成功')
    }
    
    this.socket.onmessage = (e) => {
      const message = JSON.parse(e.data)
      // 通过事件总线传递消息（需先引入eventBus）
      uni.$emit('websocket-message', message)
    }
    
    this.socket.onerror = (e) => {
      console.error('WebSocket错误:', e)
      this.reconnect(token)
    }
    
    this.socket.onclose = () => {
      this.isConnected = false
      this.reconnect(token)
    }
  }

  send(message) {
    if (this.isConnected) {
      this.socket.send(JSON.stringify(message))
    } else {
      console.error('WebSocket未连接')
    }
  }

  // utils/websocket.js
  export class WebSocketManager {
    constructor(url) {
      this.url = url;
      this.listeners = [];
    }
    connect() { /* 封装连接逻辑 */ }
    // 其他方法...
  }